<?php
class database
{
    function __construct()
    {
        $serveur = mysql_connect("localhost", "root", "");
        if (!$serveur) 
        {
            die('Non connecté : ' . mysql_error());
        }
        $db_exemple1 = mysql_select_db('exalog', $serveur);
        if (!$db_exemple1) 
        {
            die('Impossible d/utiliser la base : ' . mysql_error());
        }
    }
    
    function select($table,$id)
    {
        $query = "select * from ".$table." where id_societe = ".$id;
        $result = mysql_query($query);
        
        return $result;
    }
    
    function insert()
    {
        $columns="";
        $values="";
         $numargs = func_num_args();
    
            if (($numargs% 2) !=0) 
            {
                for($i=1;$i<=($numargs/2);$i++)
                {
                    if($columns == "")
                        $columns = func_get_arg(1);
                    else
                        $columns .= ",".func_get_arg($i);
                }
                
                for($i<=($numargs/2)+1;$i<=$numargs-1;$i++)
                {
                    if($values == "")
                        $values = func_get_arg($i);
                    else
                        $values .= "','".func_get_arg($i);
                }

            }        
        $query ="INSERT INTO `".func_get_arg(0)."` ($columns)
        VALUES ('".$values."')";
        $result = mysql_query($query);
        
        if (!$result) 
        {
            die($query. 'probleme insert : ' . mysql_error());
        }
        
        return mysql_insert_id(); 
    }
    
    function update()
    {
        $setValues = "";
        $numargs = func_num_args();
        for($i = 2;$i<$numargs;$i=$i+2) 
        {
            if($setValues == "")
            {
                $setValues = func_get_arg($i)." = '".func_get_arg($i+1)."'"; 
            }
            else
            {
                $setValues .= ",".func_get_arg($i)." = '".func_get_arg($i+1)."'";
            }
        }
        $query = "UPDATE ".func_get_arg(1)." SET ".$setValues." WHERE id_societe = ".func_get_arg(0);
        mysql_query("UPDATE ".func_get_arg(1)." SET ".$setValues." WHERE id_societe = ".func_get_arg(0));
        //mysql_query("UPDATE bd SET e-mail = $email, service = $service, nom = $nom WHERE id = $id");
        return $query;
    }
}
?>
